package com.rub.android.ui.activity

import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import android.app.PendingIntent

import android.content.Intent

import android.app.Notification
import android.graphics.BitmapFactory
import android.graphics.Color
import android.view.View
import com.rub.android.R

/**
 * 此通知必须要用户开启通知权限才可以收到.
 */
class NotificationActivity : AppCompatActivity() {

    private lateinit var notificationManager: NotificationManager
    private lateinit var notification: Notification

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_notification)

        notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager

        //判断SDK版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationChannel = NotificationChannel(
                "rubId",
                getString(R.string.rubTip),
                NotificationManager.IMPORTANCE_HIGH
            )
            notificationManager.createNotificationChannel(notificationChannel)
        }

        val intent = Intent(this, LoginActivity::class.java)
        val pIntent = PendingIntent.getActivity(this, 1, intent, 0)

        //rubId要与NotificationChannel的Id对应上.
        notification = NotificationCompat.Builder(this, "rubId")
            .setContentTitle("蹭蹭好棒")
            .setContentText("蹭蹭好贤惠，真的是一个好女人,好媳妇.")
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.monkey)
            .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.rub))
            .setColor(Color.parseColor("#FF0000"))
            .setAutoCancel(true)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setContentIntent(pIntent)
            .build()

        //notificationManager?.notify(123, notification)
    }


    fun sendNotification(v: View) {
        //notify与cancel的Id必须要一样
        notificationManager?.notify(123, notification)
    }

    fun cancelNotification(v: View) {
        notificationManager.cancel(123)
    }

}